#include <iostream>
#include <cstdint>
#include <unistd.h>
#include <sys/timerfd.h>

int main()
{
    // int timerfd_create(int clockid, int flags);
    int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
    if (timerfd < 0)
    {
        perror("timerfd_create error");
        return -1;
    }
    struct itimerspec itime;
    itime.it_value = {1, 0};
    itime.it_interval = {1, 0};
    timerfd_settime(timerfd, 0, &itime, nullptr);

    while (1)
    {
        uint64_t times;
        int n = read(timerfd, &times, sizeof(times));
        if (n < 0)
        {
            perror("read error");
            return -1;
        }
        printf("超时了，距离上一次超时了%ld次\n", times);
    }
    close(timerfd);
    return 0;
}